package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.tabs.TabLayout;

import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;


import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPersonShopBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.view.ui.fragment.packers.PersonCommonFragment;
import com.sgb.kjwl.view.ui.fragment.packers.PersonIntroFragment;
import com.sgb.kjwl.viewmodel.self.packers.PersonShopModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.DateUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

/**
 *
 * @author WYJ
 * @date 2020/9/10
 * description:选择工队人员
 */
public class PersonShopActivity extends AppActivity {

    @Inject
    PersonShopModel mModel;
    private ActivityPersonShopBinding mBind;

    private String mPersonUserNo;
    private String mPersonNo;

    public static void start(Context context, String no,String userNo, Long time, String title) {
        Intent intent = new Intent();
        intent.putExtra("no", no);
        intent.putExtra("userNo", userNo);
        intent.putExtra("publishTime", time);
        intent.putExtra("title", title);
        intent.setClass(context, PersonShopActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent
                .builder()
                .appComponent(appComponent)
                .packersModule(new PackersModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setTitle(getIntent().getStringExtra("title"));
    }

    @Override
    protected void initView() {
        mPersonUserNo = getIntent().getStringExtra("userNo");
        mPersonNo = getIntent().getStringExtra("no");
        long publishTime = getIntent().getLongExtra("publishTime", 0);
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_person_shop);
        mBind.setLifecycleOwner(this);
        mBind.setViewModel(null);
        if (publishTime > 0) {
            mBind.tvTime.setText(String.format(Locale.CHINA,"%s发布",DateUtils.timestampToDate(publishTime)));
        } else {
            mBind.tvTime.setVisibility(View.INVISIBLE);
        }
        initTab();
    }

    private void initTab() {
        mBind.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBind.tabLayout.setupWithViewPager(mBind.vp);
        // teamInfo:工队简介
        // teamPublish:我的发布
        // teamOffer:工种报价
        // teamAchieve:施工业绩
        // teamMember:工队人员
        // teamEquipment:自有设备
        List<MenuEntity<Object>> mTabs = new ArrayList<>();
        MenuEntity<Object> menu1 = new MenuEntity<>();
        MenuEntity<Object> menu3 = new MenuEntity<>();
        MenuEntity<Object> menu4 = new MenuEntity<>();
        MenuEntity<Object> menu5 = new MenuEntity<>();
        MenuEntity<Object> menu6 = new MenuEntity<>();
        MenuEntity<Object> menu7 = new MenuEntity<>();
        menu1.setId("teamInfo");
        menu1.setName(getResources().getString(R.string.self_my_team));
        menu3.setId("teamOffer");
        menu3.setName(getResources().getString(R.string.self_team6));
        menu4.setId("teamAchieve");
        menu4.setName(getResources().getString(R.string.self_team7));
        menu5.setId("teamMember");
        menu5.setName(getResources().getString(R.string.self_team8));
        menu6.setId("teamEquipment");
        menu6.setName(getResources().getString(R.string.self_team2));
        menu7.setId("teamAlbum");
        menu7.setName(getResources().getString(R.string.self_team9));
        mTabs.add(menu1);
        mTabs.add(menu3);
        mTabs.add(menu4);
        mTabs.add(menu5);
        mTabs.add(menu6);
        mTabs.add(menu7);

        ArrayList<Fragment> fragments = new ArrayList<>();
        String[] mTitles = new String[mTabs.size()];
        for (int i = 0; i < mTabs.size(); i++) {
            mTitles[i] = mTabs.get(i).getName();
            if (i == 0) {
                fragments.add(PersonIntroFragment.newInstance(mPersonUserNo,mPersonNo, mTabs.get(i).getId()));
            } else {
                fragments.add(PersonCommonFragment.newInstance(mPersonUserNo, mPersonNo,mTabs.get(i).getId()));
            }
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), fragments, mTitles);
        mBind.vp.setOffscreenPageLimit(mTitles.length);
        mBind.vp.setAdapter(mPagerAdapter);
    }

    public void updatePersonInfo(ShopTeamInfoEntity entity) {
        if (entity == null){
            return;
        }

        if (!TextUtils.isEmpty(entity.getAvatarAddress())) {
            RoundedCorners roundedCorners = new RoundedCorners(8);
//通过RequestOptions扩展功能,override采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions
                    .bitmapTransform(roundedCorners).override(300, 300)
                    .placeholder(R.mipmap.packer_avatar)
                    .error(R.mipmap.packer_avatar);
            Glide.with(this)
                    .load(entity.getAvatarAddress())
                    .apply(options)
                    .into(mBind.shopImg);
        }

        if (!TextUtils.isEmpty(entity.getAddress())) {
            mBind.addressLayout.setVisibility(View.VISIBLE);
           String pcA= entity.getProvinceName()+entity.getCityName()+ entity.getAreaName();
           String pc = entity.getAreaName();
            //包含省市区
           if (!TextUtils.isEmpty(pcA)&&entity.getAddress().contains(pcA)){
               mBind.tvPersonAddress.setText(entity.getAddress());
           }else {
               String address;
               if (entity.getAddress().contains(pc)){
                   address = entity.getAddress().replace(pc,"");
               }else {
                   address = entity.getAddress();
               }
               //不包含省市区处理
               mBind.tvPersonAddress.setText(String.format("%s%s",pcA,address));
           }

        } else {
            mBind.addressLayout.setVisibility(View.GONE);
        }
        switch (entity.getTeamType()) {
            case 0:
                mBind.volunteerTeamType.setText("施工队");
                break;
            case 1:
                mBind.volunteerTeamType.setText("班组工队");
                break;
            case 2:
                mBind.volunteerTeamType.setText("承包商");
                break;
            default:
                break;
        }
        if (!TextUtils.isEmpty( entity.getCategory())){
            String categoryName = entity.getContact();
            if (!TextUtils.isEmpty(categoryName)){
                mBind.tvPersonName.setText(String.format("%s--%s",categoryName,entity.getCategory()+mBind.volunteerTeamType.getText().toString()));
            }else {
                mBind.tvPersonName.setText(String.format("%s%s",entity.getCategory(),mBind.volunteerTeamType.getText().toString()));
            }
        }
        if (!TextUtils.isEmpty(entity.getCategory())) {
            mBind.projectType.setVisibility(View.VISIBLE);
            mBind.projectType.setText(entity.getCategory());
        } else {
            mBind.projectType.setVisibility(View.GONE);
        }

    }
}
